package com.lute.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.security.oauth2.common.json.JSONArray;
import org.springframework.security.oauth2.common.json.JSONException;
import org.springframework.security.oauth2.common.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lute.model.User;
import com.lute.model.UserAccountState;
import com.lute.model.UserEntitlement;
import com.lute.report.LuteReportType;
import com.lute.report.LuteSummaryReport;
import com.lute.utils.ServerErrorResponse;

@Controller
public class GetReportServlet {
	
	@RequestMapping (value="/getReport",method= RequestMethod.POST, headers = "Content-type=application/json")
	public @ResponseBody String getReport(@RequestBody String dataJSON, HttpServletRequest request) throws JSONException {
		String result = "";
		JSONArray froms;
		JSONArray employees;
		String reportType;
		String reportFormat;
		JSONObject jsonReq;
		JSONObject jsonRes = new JSONObject();
		
		
		HttpSession session = request.getSession(false);
		if(session == null) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_LOGGED_IN.toString());
			result = jsonRes.toString();
			return result;
		}
		
		try {
			jsonReq = new JSONObject(dataJSON);
			employees = jsonReq.getJSONArray("employees");
			froms = jsonReq.getJSONArray("froms");
			reportType = jsonReq.getString("reportType");
			reportFormat = jsonReq.getString("reportFormat");
		} catch(JSONException e) {
			jsonRes.put("result", ServerErrorResponse.MALFORMED_REQUEST.toString());
			result = jsonRes.toString();
			return result;
		}
		
		/* check if json data was not malformed */
		if((jsonReq.isNull("employees")) &&  (jsonReq.isNull("froms")) && 
		   (jsonReq.isNull("reportType"))  &&  (jsonReq.isNull("format"))) {
			jsonRes.put("result",ServerErrorResponse.MALFORMED_REQUEST.toString());
			result = jsonRes.toString();
			return result;
		}
		
		String role = (String)session.getAttribute("role");
		
		/* check  authorization */
		if(!(((role.equals("employee")) || (role.equals("approver")) || (role.equals("clientAdmin")))))  {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_ENTITLED.toString());
			result = jsonRes.toString();
			System.out.println(result);
			return result;
		}
		
		List<Integer> employeeIds = new ArrayList<Integer>();
		List<String> calFroms = new ArrayList<String>();
		for(int i = 0; i < employees.length(); i++) {
			employeeIds.add((Integer)employees.get(i));
		}
		for(int i = 0; i < froms.length(); i++) {
			calFroms.add((String)froms.get(i));
		}
		
		
		session.setAttribute("format", reportFormat);
		session.setAttribute("employees", employeeIds);
		session.setAttribute( "froms", calFroms);
		
		try {
			
			if(reportType.equals(LuteReportType.SUMMARY_REPORT.toString())) {
				jsonRes.put("urlPostfix", "summaryReport");
			} else if(reportType.equals(LuteReportType.CLIENT_REPORT.toString())) {
				jsonRes.put("urlPostfix", "clientReport");
			} else if(reportType.equals(LuteReportType.APPROVER_MONTHLY_SUMMARY_REPORT.toString())) {
				jsonRes.put("urlPostfix", "monthlyReport");
			} else if(reportType.equals(LuteReportType.TAX_OFFICE.toString())) {
				//TODO
			} else if(reportType.equals(LuteReportType.PAYROLL_REPORT.toString())) {
				//TODO
			} else {
				jsonRes.put("result",ServerErrorResponse.MALFORMED_REQUEST.toString());
				result = jsonRes.toString();
				return result;
			}
		}catch(NullPointerException npe) {
			jsonRes.put("result", ServerErrorResponse.INTERNAL_ERROR.toString());
			result = jsonRes.toString();
			return result;
		}
		jsonRes.put("result","OK");
		result = jsonRes.toString();
		return result;
	}
}